package zust.se.delicious.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import zust.se.delicious.dao.UserDao;
import zust.se.delicious.domain.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	@Override
	public User get(Integer id) {
		return getHibernateTemplate().get(User.class, id);
	}

	@Override
	public Integer save(User User) {
		return (Integer) getHibernateTemplate().save(User);
	}

	@Override
	public void update(User User) {
		getHibernateTemplate().update(User);

	}

	@Override
	public void delete(User User) {
		getHibernateTemplate().delete(User);

	}

	@Override
	public void delete(Integer id) {
		getHibernateTemplate().delete(get(id));

	}

	@Override
	public List<User> findAll() {
		return (List<User>) getHibernateTemplate().find("from User");
	}

	@Override
	public User findByNameAndPass(String username, String password) {
		List<User> pays = (List<User>) getHibernateTemplate().find("from User as p where p.username=? and p.password=?",
				new Object[] { username, password });
		if (pays != null && pays.size() > 0) {
			return pays.get(0);
		}
		return null;
	}
}
